﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CommonTagTime.Business;
using CommonTagTime.ModelObject;
using CommonTagTime;
using System.IO;
using System.Drawing;
using System.Web.UI.HtmlControls;

public partial class admin_ContentProductDetail : SitePage
{
    #region Fields
    public string Command
    {

        get
        {
            object o = ViewState["Command"];
            if (o != null) return (string)o;
            return string.Empty;
        }
        set { ViewState["Command"] = value; }

    }  
    #endregion

    #region Init,Load
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            
            LoadData();
        }
    }
    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        InitData();
    }
    protected override void OnInitComplete(EventArgs e)
    {
        base.OnInitComplete(e);

    }
    #endregion

    #region Methods
    private void LoadData()
    {
        if (this.Command == "edit")
        {
            int pageID = clsQueryString.GetInt("id");
            busContentProduct objBus = new busContentProduct();
            ContentProduct objData = objBus.GetByID(pageID);
            if (objData != null)
            {
                txtContent.Text = objData.Content;
                txtMetaDescription.Text = objData.MetaDescription;
                txtMetaKeyword.Text = objData.MetaKeywords;
                txtName.Text = objData.Name;
                txtTitle.Text = objData.Title;
                txtUrl.Text = objData.PageURL.Replace("products/", "");
                drpLanguage.SelectedValue = objData.Language.ToString();
            }
        }
    }
    private void InitData()
    {
        string validateGroup = "tipDetail";
        vsumContentProduct.ValidationGroup = btnSave.ValidationGroup = validateGroup;
        reqTitle.ValidationGroup = reqName.ValidationGroup = reqUrl.ValidationGroup = validateGroup;
        //Init message
        reqTitle.ErrorMessage = string.Format(Resources.Resource.MSG_REQUIREMENT, "Title page");
        reqName.ErrorMessage = string.Format(Resources.Resource.MSG_REQUIREMENT, "Name");
        reqUrl.ErrorMessage = string.Format(Resources.Resource.MSG_REQUIREMENT, "Url");
        this.Command = clsQueryString.GetString("act");
    }
    private void UpdateContentProduct(ContentProduct objContentProduct)
    {
        busContentProduct objBus = new busContentProduct();
        objContentProduct.PageId = clsQueryString.GetInt("id");
        int result = objBus.Update(objContentProduct);
        if (result == 1)
            Response.Redirect("ContentProductList.aspx?" + this.GetPageQuery + "&lang=" + drpLanguage.SelectedValue);
        else if (result == -1)//Item is Exists
            lblErrorMessage.Text = string.Format(Resources.Resource.MSG_CHECK_EXISTS, "Url");
        else//Insert Error
            lblErrorMessage.Text = Resources.Resource.MSG_UPDATE_ERROR;
    }
    private void InsertContentProduct(ContentProduct objContentProduct)
    {
        busContentProduct objBus = new busContentProduct();
        int result = objBus.Insert(objContentProduct);
        if (result == 1)
            Response.Redirect("ContentProductList.aspx?" + this.GetPageQuery + "&lang=" + drpLanguage.SelectedValue);
        else if (result == -1)//Item is Exists
            lblErrorMessage.Text = string.Format(Resources.Resource.MSG_CHECK_EXISTS, "Url");
        else//Insert Error
            lblErrorMessage.Text = Resources.Resource.MSG_INSERT_ERROR;
    }
    #endregion

    #region Events
    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            //get datavalue
            ContentProduct objData = new ContentProduct();
            objData.CreatedBy = this.userloginName;
            objData.CreatedDate = clsDateTime.Now;
            objData.PageURL = "products/" + txtUrl.Text.Trim();
            objData.Name = txtName.Text.Trim();
            objData.Title = txtTitle.Text.Trim();
            objData.Content = txtContent.Text.Trim();
            objData.MetaDescription = txtMetaDescription.Text.Trim();
            objData.MetaKeywords = txtMetaKeyword.Text.Trim();
            objData.Language = Convert.ToInt16(drpLanguage.SelectedValue);
            objData.UpdatedBy = this.userloginName;
            objData.UpdatedDate = clsDateTime.Now;
            if (this.Command == "edit")
                UpdateContentProduct(objData);
            else
                InsertContentProduct(objData);
        }
        else
        {
            vsumContentProduct.ShowMessageBox = false;
            vsumContentProduct.ShowSummary = true;
        }
    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        Response.Redirect("ContentProductList.aspx?" + this.GetPageQuery + "&lang=" + drpLanguage.SelectedValue);
    }
    #endregion

}